package com.jy.rhin.support.interceptor.convertor;

import cn.org.chiss.rhin._2015.document.repository.RetrieveDocumentSet;
import cn.org.chiss.rhin._2015.document.repository.RetrieveDocumentSetResponse;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("AuditInfoConverter4TerminologyRetrieveDocumentSet")
public class AuditInfoConverter4TerminologyRetrieveDocumentSet extends BaseAuditInfoConverter<RetrieveDocumentSet, RetrieveDocumentSetResponse> {

    public AuditInfoConverter4TerminologyRetrieveDocumentSet() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.DOCUMENTS_STORAGE,
                EventTypeCodeEnum.RETRIEVE_DOCUMENTS,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.MASTER_FILE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-storage-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_STORAGE.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(RetrieveDocumentSet retrieveDocumentSet, RetrieveDocumentSetResponse retrieveDocumentSetResponse) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(RetrieveDocumentSet retrieveDocumentSet, RetrieveDocumentSetResponse retrieveDocumentSetResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(RetrieveDocumentSet retrieveDocumentSet, RetrieveDocumentSetResponse retrieveDocumentSetResponse) {
        return new AuditParticipantUserDesc("documents-storage-service", ServiceCodeEnum.DOCUMENTS_STORAGE.getName());
    }

    @Override
    String getObjectId(RetrieveDocumentSet retrieveDocumentSet, RetrieveDocumentSetResponse retrieveDocumentSetResponse) {
        if (Objects.nonNull(retrieveDocumentSet) && !CollectionUtils.isEmpty(retrieveDocumentSet.getDocumentReference())) {
            return String.join(",", retrieveDocumentSet.getDocumentReference().stream()
                    .map(o -> Converter.toString(o.getDocumentUniqueId())).collect(Collectors.toList()));
        }
        if (Objects.nonNull(retrieveDocumentSetResponse) && !CollectionUtils.isEmpty(retrieveDocumentSetResponse.getDocument())) {
            return String.join(",", retrieveDocumentSetResponse.getDocument().stream()
                    .map(o -> Converter.toString(o.getDocumentUniqueId())).collect(Collectors.toList()));
        }
        return "";
    }
    
    @Override
   	public boolean isMatch(String method, Object data) {
   		return "DocumentStoragePortImpl.retrieveDocumentSet".equals(method);
   	}
}
